/*
 *所有关于fwb_online_good_info类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*fwb_online_good_infoService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.MyTable.IRepositories;
using System.Collections.Generic;
using System.Net;
using VOL.Core.DBManager;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
using System;

namespace VOL.MyTable.Services
{
    public partial class fwb_online_good_infoService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Ifwb_online_good_infoRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public fwb_online_good_infoService(
            Ifwb_online_good_infoRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override WebResponseContent Update(SaveModel saveModel)
        {
            string style = (string)saveModel.MainData["style"];
            WebResponseContent webResponse = new WebResponseContent();
            UpdateOnExecuting = (fwb_online_good_info fwb_online_good_info, object addList, object updateList, List<object> delKeys) =>
            {
                fwb_prize_draw_info fwb_prize_draw_info = DBServerProvider.DbContext.Set<fwb_prize_draw_info>().Where(x => x.fwb_onLine_good_reputation_id == fwb_online_good_info.fwb_onLine_good_reputation_id).FirstOrDefault();
                //   fwb_prize_draw_info.status = 2;
                // DBServerProvider.DbContext.Update(fwb_prize_draw_info);
                int status = 0;
                if (style.Equal("reject"))
                {
                    status = 0;
                    fwb_prize_list fwb_prize_list = DBServerProvider.DbContext.Set<fwb_prize_list>().Where(x => x.fwb_prize_list_id == fwb_prize_draw_info.fwb_prize_list_id).FirstOrDefault();
                    int prize_number = int.Parse(fwb_prize_list.prize_number) + 1;
                    string sql1 = "update fwb_prize_list set prize_number = @prize_number where fwb_prize_list_id = @fwb_prize_list_id";
                    DBServerProvider.SqlDapper.ExcuteNonQuery(sql1, new { prize_number, fwb_prize_list.fwb_prize_list_id });
                }
                else
                {
                    status = 2;
                }
                string sql = "update fwb_prize_draw_info set [status] = @status where fwb_prize_draw_info_id = @fwb_prize_draw_info_id";
                DBServerProvider.SqlDapper.ExcuteNonQuery(sql, new { status, fwb_prize_draw_info.fwb_prize_draw_info_id });

               webResponse.Code = "-1";
                return webResponse.OK("保存成功！");
            };
            return base.Update(saveModel);
        }
        public override WebResponseContent Export(PageDataOptions pageData)
        {
            ExportColumns = x => new {
               x.warranty_number,
               x.status,
                x.evaluater_name,
                x.vin,
               x.CreateDate,
               x.contact_address,
               x.phone_number,
               x.evaluate_mark,
               x.evaluate_opinion,
               x.prize_name,
               x.winner_name,
               x.winner_phone_number,
               x.winner_delivery_address
            };
            return base.Export(pageData);
        }
    }
}
